package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.mapper.QuestionMapper;
import com.tanhua.model.db.Question;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class QuestionApiImpl implements QuestionApi {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public Question findUserById(Long id) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Question question = questionMapper.selectOne(queryWrapper);
        return question;
    }

    /**
     * 设置陌生人问题
     * @return
     */
    @Override
    public void save(Question question) {
        // 通过id查看是否存在 陌生人问题设置
        Question questionInDb = findUserById(question.getUserId());

        if(questionInDb != null){
            // 更新操作
            question.setId(questionInDb.getId());
            questionMapper.updateById(question);
        }else {
            // 插入操作
            questionMapper.insert(question);
        }

    }
}
